package com.mk.community.controller;

import com.mk.community.service.AlphaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/26 4:01 下午
 * @desc
 */
@Api(tags = "测试")
@Controller
@RequestMapping("/alpha")
public class AlphaController {
    @Autowired
    private AlphaService alphaService1;

    @Autowired
    @Qualifier("alpha2")
    private AlphaService alphaService2;

    @GetMapping("test")
    @ResponseBody
    public String test() {
        return "Hello,World!";
    }

    @GetMapping("test1")
    @ResponseBody
    public String test1() {
        return alphaService1.hello();
    }

    @GetMapping("test2")
    @ResponseBody
    public String test2() {
        return alphaService2.hello();
    }

    @ApiOperation("返回请求信息")
    @RequestMapping(path = "/test3", method = RequestMethod.GET)
    public void test3(HttpServletRequest request, HttpServletResponse response) {
        // 设置响应头
        response.setContentType("text/html;charset=utf-8");

        try (PrintWriter writer = response.getWriter()) {
            // 遍历请求头
            writer.write("<h1>请求头</h1>");
            writer.println("请求方式：" + request.getMethod() + "<br>");
            writer.println("请求路径：" + request.getServletPath() + "<br>");
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String k = headerNames.nextElement();
                String v = request.getHeader(k);
                writer.printf("%s: %s<br>", k, v);
            }
            writer.write("<h1>请求参数</h1>");
            // 遍历请求参数
            final Enumeration<String> parameterNames = request.getParameterNames();
            while (parameterNames.hasMoreElements()) {
                String k = parameterNames.nextElement();
                String v = request.getParameter(k);
                writer.printf("%s: %s<br>", k, v);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @ApiOperation("返回视图方式1")
    @RequestMapping(path = "/test4", method = RequestMethod.GET)
    public ModelAndView test4(@RequestParam(name = "name", required = false, defaultValue = "孔德成") String name,
                              @RequestParam(name = "school", required = false, defaultValue = "日照职业技术学院") String school) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", name);
        mav.addObject("school", school);
        mav.setViewName("/demo/testView");
        return mav;
    }

    @ApiOperation("返回视图方式2")
    @RequestMapping(path = "/test5/{name}/{school}", method = RequestMethod.GET)
    public String test5(@PathVariable(name = "name") String name,
                        @PathVariable(name = "school") String school,
                        Model model) {
        model.addAttribute("name", name);
        model.addAttribute("school", school);
        return "/demo/testView";
    }

    @ApiOperation("返回map")
    @RequestMapping(path = "/test6", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> test6(String name, String school) {
        HashMap<String, Object> result = new HashMap<>(2);
        result.put("name", name);
        result.put("school", school);
        return result;
    }

    @ApiOperation("返回集合")
    @RequestMapping(path = "/test7", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String, Object>> test7() {
        ArrayList<Map<String, Object>> lists = new ArrayList<>();
        HashMap<String, Object> map;
        for (int i = 0; i < 5; i++) {
            map = new HashMap<>(2);
            map.put("name", "张三" + i);
            map.put("school", "北京大学" + i);
            lists.add(map);
        }

        return lists;
    }
}
